package org.dbstar.scheduler.identifier;

import java.io.Serializable;

import org.apache.commons.lang.StringUtils;

/**
 * 默认的标识实现类
 * 
 * @author 代波
 * @since 1.0.0
 * @version 1.0.0
 * @date 2012-3-29
 */
public final class DefaultIdentifier implements Identifier, Serializable {
	private static final long serialVersionUID = -5248092610134696715L;

	private final IdentifierType identifierType;
	private final String systemIdentifier;
	private final int sequence;
	private final String stringIdentifier;

	/**
	 * @param identifierType 标识的类型
	 * @param systemIdentifier 系统标识
	 * @param sequence 序列数值
	 * @param sequenceLength 序列数值的最小长度
	 */
	public DefaultIdentifier(IdentifierType identifierType, String systemIdentifier, int sequence, int sequenceLength) {
		if (identifierType == null) {
			throw new IllegalArgumentException("identifierType is null.");
		}
		if (systemIdentifier == null) {
			throw new IllegalArgumentException("systemIdentifier is null.");
		}
		this.identifierType = identifierType;
		this.systemIdentifier = systemIdentifier;
		this.sequence = sequence;
		this.stringIdentifier = toString(identifierType, systemIdentifier, sequence, sequenceLength);
	}

	@Override
	public IdentifierType getIdentifierType() {
		return identifierType;
	}

	@Override
	public String getSystemIdentifier() {
		return systemIdentifier;
	}

	@Override
	public int getSequence() {
		return sequence;
	}

	@Override
	public String getStringIdentifier() {
		return stringIdentifier;
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((identifierType == null) ? 0 : identifierType.hashCode());
		result = prime * result + sequence;
		result = prime * result + ((systemIdentifier == null) ? 0 : systemIdentifier.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (!Identifier.class.isInstance(obj))
			return false;
		Identifier other = (Identifier) obj;
		if (sequence != other.getSequence())
			return false;
		if (!systemIdentifier.equals(other.getSystemIdentifier()))
			return false;
		if (identifierType != other.getIdentifierType())
			return false;
		return true;
	}

	@Override
	public String toString() {
		return "DefaultIdentifier [" + stringIdentifier + "]";
	}

	/**
	 * 根据标识的参数来拼装标识的字符串形式。
	 * 
	 * @param identifierType 标识的类型
	 * @param systemIdentifier 系统标识
	 * @param sequence 序列数值
	 * @param sequenceLength 序列数值的最小长度
	 * @return 标识的字符串形式
	 */
	private static String toString(IdentifierType identifierType, String systemIdentifier, int sequence,
			int sequenceLength) {
		return identifierType + "_" + systemIdentifier + "_"
				+ StringUtils.repeat("0", sequenceLength - Integer.toString(sequence).length()) + sequence;
	}

	public static Identifier valueOf(String identifier) {
		String s[] = StringUtils.split(identifier, '_');
		IdentifierType identifierType = IdentifierType.valueOf(s[0]);
		String systemIdentifier = s[1];
		int sequence = Integer.valueOf(s[2]);
		int sequenceLength = s[2].length();
		return new DefaultIdentifier(identifierType, systemIdentifier, sequence, sequenceLength);
	}

	@Override
	public int compareTo(Identifier o) {
		return stringIdentifier.compareTo(o.getStringIdentifier());
	}
}
